package com.cloud.punish.tasklist.service.impl;

import com.cloud.punish.tasklist.entity.TaskListMenu;
import com.cloud.punish.tasklist.service.TaskListMenuCalculateService;
import com.cloud.punish.workflow.entity.WfActInst;
import com.cloud.punish.workflow.enums.WfPartEnum;
import com.cloud.punish.workflow.mapper.WfActInstMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TaskListMenuCalculateServiceImpl implements TaskListMenuCalculateService {

    @Autowired
    WfActInstMapper wfActInstMapper;

    @Override
    public boolean transit(long actId) {
        WfActInst inst = wfActInstMapper.selectByPrimaryKey(actId);
        // 节点办理类型必须是人 并且 不是结束节点
        return (inst.getActDefAttrId() & 4) != 4 && WfPartEnum.HUMAN.getPartType().equals(inst.getPartType());
    }

    @Override
    public boolean rollback(long actId) {
        // 开始节点不显示回退操作
        WfActInst inst = wfActInstMapper.selectByPrimaryKey(actId);
        return (inst.getActDefAttrId() & 2) != 2 && WfPartEnum.HUMAN.getPartType().equals(inst.getPartType());
    }

    @Override
    public boolean assign(long actId) {
        //在参与者类型不是HUMAN的情况下才显示办理菜单
        WfActInst inst = wfActInstMapper.selectByPrimaryKey(actId);
        return !WfPartEnum.HUMAN.getPartType().equals(inst.getPartType());
    }

    @Override
    public boolean cancel(long actId) {
        //在参与者类型是HUMAN的情况下才能显示
        WfActInst inst = wfActInstMapper.selectByPrimaryKey(actId);
        return WfPartEnum.HUMAN.getPartType().equals(inst.getPartType());
    }

    @Override
    public boolean finish(long actId) {
        //在参与者类型是HUMAN的情况下才能显示
        WfActInst inst = wfActInstMapper.selectByPrimaryKey(actId);
        return WfPartEnum.HUMAN.getPartType().equals(inst.getPartType());
    }

    @Override
    public List<TaskListMenu> getTaskListMenus(Long actId, List<TaskListMenu> taskListMenus) {
        if(actId == null || actId == 0){
            return taskListMenus;
        }else {
            List<TaskListMenu> result = new ArrayList<>();
            for (TaskListMenu menu : taskListMenus) {
                if (StringUtils.isNotEmpty(menu.getCalcName())) {
                    switch (menu.getCalcName()) {
                        case "transit":
                            if (transit(actId)) {
                                result.add(menu);
                            }
                            break;
                        case "rollback":
                            if (rollback(actId)) {
                                result.add(menu);
                            }
                            break;
                        case "assign":
                            if (assign(actId)) {
                                result.add(menu);
                            }
                            break;
                        case "cancel":
                            if (cancel(actId)) {
                                result.add(menu);
                            }
                            break;
                        case "finish":
                            if (finish(actId)) {
                                result.add(menu);
                            }
                            break;

                    }
                } else {
                    result.add(menu);
                }
            }
            return result;
        }

    }

}
